var exam = {
		init : function() {
			exam.examGrid=$('#examGrid');
			exam.examAddDialog = $('#examAddDialog');
			exam.examAddForm = $('#examAddForm');
			exam.examUpdateForm = $('#examUpdateForm');
			exam.examUpdateDialog = $('#examUpdateDialog');
			
			exam.examGrid.datagrid({
			 	url:window.ctx + '/ExamServlet?command=ExamList',
			 	fixed:true, 
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 5,
			    pageList: [5, 10, 15, 20],
			    pageNumber: 1,
			    sortName: 'examTimeStart',
			    sortOrder: 'ASC',
			    toolbar: '#examToolbar',
			    columns:[[
			        {field:'id', width:100, checkbox: true},
			        {field:'examTimeStart', title:'开始时间', width:100, align:'center', sortable: true},
					{field:'examTimeStop', title:'结束时间', width:100, align:'center'},
					{field:'examClassroom', title:'考试教室', width:100, align:'center'},
					{field:'qes_exam_paper__name', title:'考试科目', width:100, align:'center'},
					{field:'sys_user__name', title:'考试者', width:100, align:'center'},
					{field:'sys_class__name', title:'考试班级', width:100, align:'center'},
					{field:'operateUserId', title:'发布者ID', width:100, align:'center'},
					{field:'operateTime', title:'发布时间', width:100, align:'center'},
					
			    ]],
			    onDblClickRow: function(rowIndex, rowData) {
					exam.toolbar.showUpdateDialog(rowData.id);
				},
				
		});
		},
		toolbar: {
			showAddDialog: function() {
				console.log(exam.examAddDialog);
				exam.examAddDialog.dialog('open');
			},
			
			hiddenAddDialog: function(){
				exam.examAddDialog.dialog('close');
				exam.toolbar.resetAddForm();
			},
			
			resetAddForm: function() {
				exam.examAddForm.form('reset');
			},
			
			add: function() {
				if ($('#examAddForm').form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post( window.ctx + '/ExamServlet?command=ExamAdd',$('#examAddForm').serialize(), function(data) {
						
						if (data) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							exam.examAddDialog.dialog('close');
							$.messager.progress('close');
							exam.toolbar.resetAddForm();
							exam.examGrid.datagrid('reload');
							
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						exam.toolbar.hiddenAddDialog();
					});
				}
				
			},
			
			/*考试修改*/
			showUpdateDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx + '/ExamServlet?command=ExamGet', {id : v}, function(data) {
						if (data) {
							exam.examUpdateForm.form('load', {
								id: data.id,
								examTimeStart: data.examTimeStart,
								examTimeStop: data.examTimeStop,
								examClassroom: data.examClassroom,
								examPaperId: data.examPaperId,
								toUserIds: data.toUserIds,
								toClassId: data.toClassId,
							});
							$('#examUpdateDialog').dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = exam.examGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			
			hiddenUpdateDialog: function() {
				$('#examUpdateDialog').dialog('close');
				exam.toolbar.resetUpdateForm();
			},
			
			resetUpdateForm: function() {
				exam.examUpdateForm.form('reset');
			},
			
			
			update: function() {
				if (exam.examUpdateForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/ExamServlet?command=ExamUpdate',$("#examUpdateForm").serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							exam.toolbar.resetUpdateForm();
							exam.examGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
						exam.toolbar.hiddenUpdateDialog();
					});
				}
			},
			
			/*updateState: function() {
				var row = exam.examGrid.datagrid('getSelections');
				if (row.length == 1) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/SystemServlet?command=updateStateUser', {id : row[0].id} , function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							exam.examGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
		},*/
		
		
		
		
		}		
}

$(function(){
	exam.init();
});